/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package num;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Random;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author hongjun
 */
public class AlgManager {
    protected Set<String> s = new LinkedHashSet<String>(20);
    protected String algPool = "wxyzbcdefghjkmnpqrstuvwxyz";//don't add a,l,o,i which are confusing..
    protected String algQuestion;//don't add a,l,o,i which are confusing..
    protected Set<String> o = new LinkedHashSet<String>(20);
    protected Random random = new Random();   
    String[] AlgSingleFmt = {"\\b[0-9]*[a-zX-Z]\\b"};

    AlgManager(){
    }
    
 /**
 * Searching for all algebra characters. 
     * randomize it 
 * @author hongjun
 */
    AlgManager(String str){
        this();  
        this.algQuestion=str;
    }




    public static void main(String args[]) throws SQLException{
        
        //hj: SQRT/POW/EXP functions, make sure uppercase it.
        String str="(3+5)x+(4+3)x+(82+(82-80))";
        str = str.replaceAll("sqrt", "SQRT");
        str = str.replaceAll("pow", "POW");
        
        //hj check for xyz terms, substitute with single term
        String tmp = str + " ";
        String substr = "aloi";//for xy xyz etc...
        ArrayList<String> tosub = new ArrayList<String>();
        Pattern pattern2 =
               Pattern.compile("[^a-z][0-9]*[a-z][a-z][a-z]*[^a-z]");  

        Matcher matcher2 =
                pattern2.matcher(tmp);
        int k = 0;
        while (matcher2.find()) {
            tosub.add(tmp.substring(matcher2.start() + 1, matcher2.end() - 1));
            str = str.replace(tosub.get(k), String.valueOf(substr.charAt(k)));
            k++;
        }
        //...end

        tmp = str + " ";
        Pattern pattern =
                Pattern.compile("[^a-z][a-z][^a-z]");


        Matcher matcher =
                pattern.matcher(tmp);
        if (!matcher.find()) {
            System.out.println("not found");
        }




        char[] sss = new char[10];
        int j = 0;
        boolean endwithvar = false;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) <= 'z' && str.charAt(i) >= 'a') {
                if (i > 0 && str.charAt(i - 1) == '(') {
                     System.out.println("not found"); //I don't mess with this
                }
                sss[j] = str.charAt(i);
                j++;
                if (i == str.length() - 1) {
                    endwithvar = true;
                }
            }
        }
        if (!endwithvar) {
            j++;
        }

        if (j == 0) {
             System.out.println("not found");
        }
        String ss[] = str.split("[a-z]", 0);
        String query = "CONCAT(";
        for (int i = 0; i < j; i++) {

            String signfact = "";
            char ssssign = ' ';

                if (ss[i].length() > 0) {
                if ((ss[i].charAt(ss[i].length() - 1) == '+' || ss[i].charAt(ss[i].length() - 1) == '-'
                        || ss[i].charAt(ss[i].length() - 1) == '*' || ss[i].charAt(ss[i].length() - 1) == '/')) {
                    ssssign = ss[i].charAt(ss[i].length() - 1);
                    ss[i] = ss[i].substring(0, ss[i].length() - 1);
                }
                }
                 if (ss[i].length() > 0) {
                
                  if (i > 0 &&  Integer.valueOf((1)) >= 0) {
                        signfact = "+";
                    }
                }

                query = query + "'" + signfact + "',";
                if (ss[i].length() > 0) {
                    query = query + ss[i] + ",";
                }
                if (ssssign == ' ') {
                    query = query + "'" + sss[i] + "'";
                } else {
                    query = query + "'" + ssssign + sss[i] + "'";
                }
                if (i < j - 1) {
                    query = query + ",";
                }

        }
        query = query + ")";
        for (int i = 0; i < tosub.size(); i++) {
            query = query.replace(String.valueOf(substr.charAt(i)), tosub.get(i));
        }
        
        
         System.out.println(query);
    }
  
    
}
